package com.yc.leetcode;

import java.util.*;

/**
 * 数字数组
 */
public class NumsArr {
    public static void main(String[] args) {
        NumsArr na = new NumsArr();
//        na.m1();
//        na.m2();
//        na.m3();
//        na.m4();
//        na.m5();
        na.test();

//        int a = 1;
//        int b = 3^1;
//        System.out.println(0^3);
//        StringBuilder sb = new StringBuilder();


    }

    public void test() {
        int[] a = {1,2,3,4,5};
        System.out.println('R'^'D');
    }

    public void m1() {
        int[] nums = {0,4,0,3,2};
        int k = 1;
        System.out.println(findMaxAverage(nums, k));
    }

    public double findMaxAverage(int[] nums, int k) {

        if (k > nums.length) {//数组长度不够，无法找到长度为k的子数组
            return 0.0;
        }

        int maxSum = 0;
        int sum = 0;
        for (int i =0; i<k; i++) {
            sum += nums[i];
        }
        maxSum = sum;
        for (int i=k; i<nums.length; i++) {
            sum = sum + nums[i] - nums[i-k];
            maxSum = Math.max(sum, maxSum);
        }
        return 1.0*maxSum/k;
    }

    public void m2() {
        int[] nums = {1,0,0,1,1,0};
        System.out.println(longestOnes(nums, 2));
    }

    public int longestOnes(int[] nums, int k) {
        int n = nums.length;
        int left = 0, right = 0;
        int zeroNum = 0;
        while(right<n) {
            if(nums[right] == 0) {
                zeroNum++;
            }
            if(zeroNum > k) {
                //?
                if(nums[left]==0) {
                    zeroNum--;
                }
                left++;
            }
            right++;
        }
        return right-left;
    }

    public void m3() {
        int[] nums = {1,7,3,6,5,6};
        System.out.println(pivotIndex(nums));
    }

    public int pivotIndex(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        for (int i=1; i<n; i++) {
            dp[i] = dp[i-1] + nums[i-1];
        }

        int right = 0;
        int mid = (dp[n-1] == 0)?(n-1):-1;
        for (int i=n-2; i>=0;i--) {
            right += nums[i+1];
            mid = (dp[i]==right)?i:-1;
        }
        return mid;
    }

    public void m4() {
        String w1 = "cabbba";
        String w2 = "abbccc";
        System.out.println(closeStrings(w1,w2));
    }

    public boolean closeStrings(String word1, String word2) {

        char[] cs1 = new char[26];
        for (int i=0; i<word1.length(); i++) {
            cs1[word1.charAt(i)-'a']++;
        }

        char[] cs2 = new char[26];
        for (int i=0; i<word2.length(); i++) {
            cs2[word2.charAt(i)-'a']++;
        }

        //判断类型是否一致
        for (int i=0; i<26; i++) {
            if ((cs1[i]>0) ^ (cs2[i]>0)) {
                return false;
            }
        }

        //判断数量是否一致
        Arrays.sort(cs1);
        Arrays.sort(cs2);

        for (int i=0; i<26; i++) {
            if (cs1[i] != cs2[i]) {
                return false;
            }
        }
        return true;

    }

    public void m5() {
        int [][] g = {{3,2,1},{1,7,6},{2,7,7}};
        System.out.println(equalPairs(g));
    }


    public int equalPairs(int[][] grid) {
        int n = grid.length;

        //获得转置数组
        int[][] grid2 = new int[n][n];
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                grid2[j][i] = grid[i][j];
            }
        }
        //grid的行 和 grid2的列做比较
        int res = 0;
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                if (Arrays.equals(grid2[j], grid[i])) {
                    res++;
                }
            }
        }
        return res;

    }
}
